/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tthdhm.truyvetduongdi.dao;

import com.tthdhm.truyvetduongdi.pojo.CustomerPOJO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import com.tthdhm.truyvetduongdi.util.Constants;
import java.sql.ResultSet;

/**
 *
 * @author BPT
 */
public class CustomerDAO {
    private static CustomerDAO instance;

    private CustomerDAO() {
    }

    public static CustomerDAO getInstance() {
        if (instance == null) {
            instance = new CustomerDAO();
        }
        return instance;
    }
    
    public CustomerPOJO getCustomer(String username,String password){
        CustomerPOJO customerPOJO=null;
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        try {
            query = "Select * from Customer where Username=? and Password=?";
            conn = com.tthdhm.truyvetduongdi.util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setString(1, username);
                prepareStatement.setString(2, password);
            } else {
                return customerPOJO;
            }
            prepareStatement.execute();
            rs = prepareStatement.getResultSet();
            if(rs.next()) {
                customerPOJO = new CustomerPOJO();
                // Set 
                customerPOJO.setUsername(username);
                customerPOJO.setPassword(rs.getString("Password"));
                customerPOJO.setFullname(rs.getString("Fullname"));
                customerPOJO.setSex(rs.getInt("Sex"));
                customerPOJO.setStatus(rs.getInt("Status"));
                customerPOJO.setTelephone(rs.getString("Telephone"));
                customerPOJO.setAddress(rs.getString("Address"));
                customerPOJO.setBirthday(rs.getDate("Birthday"));
                customerPOJO.setEmail(rs.getString("Email"));
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
        } finally {
            try {
                rs.close();
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
        return customerPOJO;
    }
    
    public CustomerPOJO getCustomer(String username){
        CustomerPOJO customerPOJO=null;
        Connection conn = null;
        PreparedStatement prepareStatement=null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        try {
            query = "Select * from Customer where Username=?";
            conn = com.tthdhm.truyvetduongdi.util.Connection.getConnection();
            if (conn != null) {
                prepareStatement = conn.prepareStatement(query);
                prepareStatement.setString(1, username);
            } else {
                return customerPOJO;
            }
            prepareStatement.execute();
            rs = prepareStatement.getResultSet();
            if(rs.next()) {
                customerPOJO = new CustomerPOJO();
                // Set 
                customerPOJO.setUsername(username);
                customerPOJO.setPassword(rs.getString("Password"));
                customerPOJO.setFullname(rs.getString("Fullname"));
                customerPOJO.setSex(rs.getInt("Sex"));
                customerPOJO.setStatus(rs.getInt("Status"));
                customerPOJO.setTelephone(rs.getString("Telephone"));
                customerPOJO.setAddress(rs.getString("Address"));
                customerPOJO.setBirthday(rs.getDate("Birthday"));
                customerPOJO.setEmail(rs.getString("Email"));
            }

        } catch (Exception ex) {
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:" + ex);
        } finally {
            try {
                rs.close();
                prepareStatement.close();
                conn.close();
            } catch (Exception ex) {
//                LogSystem.getInstance().info(
//                        "BookDAO.getAllBook: " + sqlEx);
            }
//            LogSystem.getInstance().info(
//                    "BookDAO.getAllBook:End");
        }
        return customerPOJO;
    }
}
